<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;


use app\models\CrmSystemDict;
use app\models\search\CrmSystemDictSearch;
use yii\helpers\ArrayHelper;

class BasicInformationSystemDictController  extends CommonController
{
    /**
     * 列表
     */
    public function actionList(){
        $model = new CrmSystemDictSearch();
        $pro = $model->search(\Yii::$app->request->post());
        $count = $pro->query->count();
        isset($_POST['limit'])?$pro->query->limit($_POST['limit']):"";
        isset($_POST['page'])?$pro->query->offset($_POST['limit']*($_POST['page']-1)):"";
        $res = $pro->query->asArray()->all();
        return $this->actionLayData($count,$res,0,"");
    }
    /**
     * 指定的列表
     */
    public function actionSpecialList(){
        $model = new CrmSystemDictSearch();
        $pro = $model->specialSearch(\Yii::$app->request->post());
        $count = $pro->query->count();
        isset($_POST['limit'])?$pro->query->limit($_POST['limit']):"";
        isset($_POST['page'])?$pro->query->offset($_POST['limit']*($_POST['page']-1)):"";
        $datas = $pro->query->select("code_type_name,display_value,id,code_id")->asArray()->all();
        $data = $datas;
        $err = 1;
        $res['data'] = $this->FetchRepeatMemberInArray($data,'code_type_name');
        $res['res'] = $err;
        return json_encode($res);
    }

    //将数组处理成同样的数组放在一个数组里面，方便管理查找，提取
    function FetchRepeatMemberInArray($array,$colum="",$colum2="")
    {
        $uniqKeys = @array_unique(array_column($array,$colum));//唯一的键值
        $repeat_arr = [];
        $merge = [];
        foreach ($uniqKeys as $i=>$vv){
            foreach ($array as $j=>$v){
                $array[$j]['value'] = $v['code_id'];
                $array[$j]['text'] = $v['display_value'];
                if ($vv != $v['code_type_name']) {
                    $repeat_arr [$i] = $v;
                }
            }
        }
        $final_arr = @array_diff_assoc($array,$repeat_arr);//过滤出来不相同的所有数组
        $uniqueArr = [];
        foreach ($final_arr as $k=>$v){//把相同的放在一起
            foreach ($uniqKeys as $kk=>$vv) {//把相同的放在一起作为一个数组
                if ($v[$colum] == $vv) {
                    $uniqueArr[$kk][$kk] = $array[$kk];
                    $uniqueArr[$kk][$k] = $v;
                }
            }
        }
        foreach ($uniqKeys as $k=>$v){//把相同的放在一起并且新增相应的参数
            $merge[$v] = $array[$k];
            $merge[$v]["children"] = $uniqueArr[$k];
            $temp = "";
            foreach ($uniqueArr[$k] as $kk=>$vv){
                if (!empty($colum2)){
                    $temp .= $vv[$colum2].",";
                }
            }
            $temp = substr($temp,0,-1);
            $merge[$v]["childrenName"] = $temp;
        }
        return  $merge;//把数组的键名修改为 标准的0,1
    }

    /**
     * 新增
     */
    public function actionAdd(){
        $model = new CrmSystemDictSearch();
        $arr['CrmSystemDictSearch']['code_type'] = isset($_POST['CrmSystemDict']['code_type'])?$_POST['CrmSystemDict']['code_type']:"";
        $arr['CrmSystemDictSearch']['flag'] = isset($_POST['CrmSystemDict']['flag'])?$_POST['CrmSystemDict']['flag']:"";
        $pro = $model->search($arr);
        $one = $pro->query->orderBy('code_id desc')->one();
        $_POST['CrmSystemDict']['code_id'] = $one->code_id+1;
        $model = new CrmSystemDict();
        if($model->load(\Yii::$app->request->post()) && $model->save()){
            $data = "新增成功";
            $err = 1;
        }else{
            $errs = $model->getErrors();
            $errArr = [];
            foreach ($errs as $k=>$v){
                $errArr[] = $v[0];
            }
            $data = $errArr;
            $err = 0;
        }
        $res['data'] = $data;
        $res['res'] = $err;
        return json_encode($res);
    }

    /**
     * 新增
     */
    public function actionAddType(){
        $model = new CrmSystemDictSearch();
        $arr['CrmSystemDictSearch']['code_type'] = isset($_POST['CrmSystemDict']['code_type'])?$_POST['CrmSystemDict']['code_type']:"";
        $arr['CrmSystemDictSearch']['flag'] = isset($_POST['CrmSystemDict']['flag'])?$_POST['CrmSystemDict']['flag']:"";
        $pro = $model->search($arr);
        $one = $pro->query->orderBy('code_type desc')->one();
        $_POST['CrmSystemDict']['code_type'] = $one->code_type+1;
        $_POST['CrmSystemDict']['code_id'] = 1;
        $model = new CrmSystemDict();
        if($model->load(\Yii::$app->request->post()) && $model->save()){
            $data = "新增成功";
            $err = 1;
        }else{
            $errs = $model->getErrors();
            $errArr = [];
            foreach ($errs as $k=>$v){
                $errArr[] = $v[0];
            }
            $data = $errArr;
            $err = 0;
        }
        $res['data'] = $data;
        $res['res'] = $err;
        return json_encode($res);
    }

    /**
     * 修改
     */
    public function actionEdit($id){
        $model = CrmSystemDict::findOne($id);
        if($model->load(\Yii::$app->request->post()) && $model->save()){
            $data = ArrayHelper::toArray($model);
            $err = 1;
        }else{
            $errs = $model->getErrors();
            $errArr = [];
            foreach ($errs as $k=>$v){
                $errArr[] = $v[0];
            }
            $data = $errArr;
            $err = 0;
        }
        $res['data'] = $data;
        $res['res'] = $err;
        return json_encode($res);
    }

    /**
     * 删除
     */
    public function actionDelete($id){
        $model = CrmSystemDict::findOne($id);
        if($model->delete()){
            $data = "删除成功";
            $err = 1;
        }else{
            $errs = $model->getErrors();
            $errArr = [];
            foreach ($errs as $k=>$v){
                $errArr[] = $v[0];
            }
            $data = $errArr;
            $err = 0;
        }
        $res['data'] = $data;
        $res['res'] = $err;
        return json_encode($res);
    }

    /**
     * 删除
     */
    public function actionDeleteType($code_type){
        if (!empty($code_type)){
            if(CrmSystemDict::deleteAll(['code_type'=>$code_type])){
                $data = "删除成功";
                $err = 1;
            }else{
                $data = "删除失败";
                $err = 0;
            }
        }else{
            $data = "删除失败";
            $err = 0;
        }
        $res['data'] = $data;
        $res['res'] = $err;
        return json_encode($res);
    }
    /**
     * 将数据打包成 layui可以识别的数据格式
     * @param $count
     * @param $all
     * @return array
     */
    public function packData($count,$all){
        $res = [];
        $res['code'] = 0;
        $res['msg'] = "";
        $res['count'] = $count;
        $res['data'] = $all;
        $res['page'] = (isset($_GET['page'])?$_GET['page']:is_set($_POST,'page'));
        return json_encode((object)$res);
    }
}